<?php namespace App\Controllers;

use CodeIgniter\Controller;
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
use \App\Andy\CheckSpam;
use \App\Andy\Utility;
use CodeIgniter\I18n\Time;



class Sendmail extends BaseController
{
       
			   protected $totalMessage;    
			   public function __construct() 
									{
										parent::__construct();
										$this->myTime = parent::getTime();
										$this->myDate= date("d/m/Y",$this->myTime); 	
									
								   }
  
  
  
  
   public function processform()
	
	{
		
		$lucky= $this->request->getVar(csrf_token());
		//above i used to check there was a token which can be echoed out  
	
     		
	
 	$cleanName= htmlentities( $this->request->getVar('name')); 
   $this->request->getPost('name');
 	$cleanEmail=  htmlentities($this->request->getVar('email'));
 	$message= $this->request->getVar('message');
	
	$spamHandle = new CheckSpam();
	$utilityHandle= new Utility();
	//CheckSpam is in app/Andy and has namespace App\Andy 
	$logic = $spamHandle->filterSpam($message);
	//before bothering to remove char queck if spam 	
					if ($logic =="true")
		{
		 $data = [
						 'title'=>'spam',
						 'date'=>$this->myDate
						  
						 
						 ];
		
		
		
		echo view('spam',$data);
		
				
		
		}
	
	       else
	       {
			   
			   //start of else bracket
	
	
	$cleanMessage= $utilityHandle->removeScript($message);
	$cleanMessage= $utilityHandle->convertQuotes($cleanMessage);
	$cleanMessage = $utilityHandle->newRegex($cleanMessage);
	
	
	$IP= $_SERVER['REMOTE_ADDR'];
	
	$theirIP= strval($IP);
	$ip= $this->request->getIPAddress();
	$this->totalMessage = "new enquiry  from AndrinaBoutique   \r\n ".$cleanName ." \r\n their ip from native php is    :".$theirIP."\r\n from CI4 get request its  ".$ip."\r\n their email is:  ".$cleanEmail." \r\n their message:  ".$cleanMessage;
	
	               
	                  $mail = new PHPMailer(true); 
	                        $mail->isSMTP();
							$mail->Timeout = 20;
							//Enable SMTP debugging, 0 = off (for production use),  1 = client messages,  2 = client and server messages
							$mail->SMTPDebug = 0;
							//i dont bother with deg bug since i get error with a try catch ,its enabled via (true) in     $mail = new PHPMailer(true); 
							//Set the hostname of the mail server  below i use google 
							$mail->Host = 'smtp.gmail.com';
							$mail->Port = 587;
							$mail->SMTPSecure = 'tls';        
							$mail->SMTPAuth = true;
							//Username to use for SMTP authentication - use full email address for gmail
							$mail->Username = "johndoe@gmail.com";
							//Password to use for SMTP authentication in my case from generated from google 2 step 
						
						$mail->Password = "xxxxxxxxxxxxxx";
						
						
						
							//Set who the message is to be sent from
							$mail->setFrom('admin@andrinadboutique.com', 'webmaster ');
							//and email for emai lto be sent 
							$mail->addAddress('johndoe@yahoo.com', ' ');
						
							$mail->Subject = 'new enquiry from andrina boutique    ';
							$mail->Body = $this->totalMessage;
							
					try{		
							
								$logic2 = $result=			$mail->send();
										   if (($logic2==1) OR ($logic2=="true"))
										 {
										  $data = ['title' => 'sending email ',
										  'info'    => 'your message was sent ',
										  'date'=>$this->myDate
												  ];
															
										 echo view ('info',$data);
										
										 }
	                                     
	                    }
	                   
	                    catch ( \Exception  $e)
			 {
			 		echo "something went wrong";	 
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'something went wrong with sending message' ,
				   'infoException'=>$e->getMessage(),
				   'date'=>$this->myDate,
					 ];
			
				echo view('info2', $data);
				 die();
			 //above try catch works  and outputs rendered issue to view 
			    }
	                    
	                    
	                                 
					     
		}//end of else bracket 
	
	
	}//end of process form method 
	
	
	
	
	
   
	
	
	
	
	
	
	
	
	
}
